package com.wtwd.device.util;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ByteUtils {

    /**
     * long 转 8位字节数组
     *
     * @param number
     * @return
     */
    public static byte[] longToBytes(long number) {
        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[7 - i] = (byte) (temp & 0xff);//
            temp >>= 8; // 向右移8位
        }
        return b;
    }


    /**
     * 8位字节数组转long
     *
     * @param b
     * @return
     */
    public static long bytesToLong(byte... b) {
        long temp = b[0];//最高位
        for (int i = 1; i < b.length; i++) {
            temp <<= 8;
            temp |= b[i] & 0xff;
        }
        return temp;
    }

    public static int bytesToInt(byte... bytes) {
        int temp = bytes[0];
        for (int i = 1; i < bytes.length; i++) {
            temp <<= 8;
            temp |= bytes[i] & 0xff;
        }
        return temp;
    }

    /**
     * int转4个字节
     * @param number
     * @return
     */
    public static byte[] intTo4Bytes(int number){
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((number & 0xff000000) >> 24);
        bytes[1] = (byte) ((number & 0xff0000) >> 16);
        bytes[2] = (byte) ((number & 0xff00) >> 8);
        bytes[3] = (byte) (number & 0xff);
        return bytes;
    }


    /**
     * int (最大65535) 转 2个字节数
     *
     * @param number
     * @return
     */
    public static byte[] intTo2Bytes(int number) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) (number >> 8);
        bytes[1] = (byte) (number & 0xff);
        return bytes;
    }

    /**
     * 2个字节 转 int 最大65535
     *
     * @param hv
     * @param lv
     * @return
     */
    public static int bytes2ToInt(byte hv, byte lv) {
        return (hv & 0xff) << 8 | lv & 0xff;
    }


    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xff);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

    public static byte[] strHexToByte(String hex) {
        String[] s = hex.split(" ");
        byte[] bytes = new byte[s.length];
        for (int i = 0; i <bytes.length ; i++) {
            bytes[i] =(byte)Integer.parseInt(s[i],16);
        }

        return bytes;
    }

    /**
     * 对象转字节数组
     *
     * @param obj
     * @return
     */
    public static byte[] ObjToBytes(Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return bytes;
    }

    /**
     * 字节数组转对象
     *
     * @param bytes
     * @return
     */
    public static Object bytesToObject(byte[] bytes) {
        Object obj = null;
        ByteArrayInputStream bi = null;
        ObjectInputStream oi = null;
        try {
            bi = new ByteArrayInputStream(bytes);
            oi = new ObjectInputStream(bi);
            obj = oi.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bi != null) {
                    bi.close();
                    oi.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return obj;
    }



    /**
     * BCD码转为10进制串(阿拉伯数据)
     *
     * @param bcd
     * @return 10进制串
     */
    public static String bcd2Str(byte... bcd) {
        StringBuilder sb = new StringBuilder();
        int startBit = bcd[0] >> 4 & 0xf;
        if (startBit != 0) {
            sb.append(startBit);
        }
        sb.append(bcd[0] & 0xf);
        for (int i = 1; i < bcd.length; i++) {
            sb.append(bcd[i] >> 4 & 0xf)
                    .append(bcd[i] & 0xf);
        }
        return sb.toString();
    }

    /**
     * 10进制串转为BCD码
     *
     * @param number
     * @return BCD码
     */
    public static byte[] str2Bcd(String number) {
        byte[] s = number.getBytes();
        int len = s.length / 2;
        boolean isFill = s.length % 2 != 0;
        byte[] b = new byte[isFill ? len + 1 : len];
        if (isFill) {
            b[0] = (byte) (s[0] & 0xf);
            for (int i = 1; i < b.length; i++) {

                b[i] = (byte) (s[2 * i - 1] << 4 | (s[2 * i] & 0xf));
            }
        } else {
            for (int i = 0; i < b.length; i++) {
                b[i] = (byte) (s[2 * i] << 4 | (s[2 * i + 1] & 0xf));
            }
        }
        return b;
    }

    /**
     * 合并字节数组
     *
     * @param bytess
     * @return
     */
    public static byte[] mergeBytes(byte[]... bytess) {
        int len = 0;
        for (byte[] bytes : bytess) {
            if (bytes == null) continue;
            len += bytes.length;
        }
        byte[] bytes = new byte[len];
        int index = 0;
        for (int i = 0; i < bytess.length; i++) {
            byte[] src = bytess[i];
            if (src == null) continue;
            System.arraycopy(src, 0, bytes, index, src.length);
            index += src.length;
        }
        return bytes;
    }

    public static String byteToHexStr(byte... data) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(" ");
            sb.append(String.format("%02x", b));
        }
        return sb.toString();

    }

    /**
     * 将字符串转换为 Unicode 十六进制编码
     * @param input 原始字符串
     * @return 十六进制编码字符串
     */
    public static String stringToHex(String input) {
        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }

    /**
     * 将 Unicode 十六进制编码转换回字符串
     * @param hex 十六进制编码字符串
     * @return 原始字符串
     */
    public static String hexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        int len = hex.length();
        for (int i = 0; i < len; i++) {
            if (hex.charAt(i) == '\\' && hex.charAt(i + 1) == 'u') {
                String unicode = hex.substring(i + 2, i + 6);
                int code = Integer.parseInt(unicode, 16);
                sb.append((char) code);
                i += 5; //
            } else {
                sb.append(hex.charAt(i));
            }
        }
        return sb.toString();
    }



    public static void main(String[] args) {
       byte[] startBit = {0x0D,0x0A,0x00};
        String s = new String(startBit, StandardCharsets.UTF_8);
        System.out.println(s);

    }

    public static int getLB(byte b) {
        return b & 0x0f;
    }
}
